﻿/*
* This software is the confidential and proprietary
* information of NetBay
* Such confidential information should not be disclosed
* NetBay copyright(c) 2010
*/
#region Using
using System;
using System.Collections.Generic;
using System.Data;
using NetBay.Core.BusinessEntities.Workflow;
using NetBay.Core.Log;
using NetBay.Core.Extensions;

#endregion

namespace Rio.Flow.Business.Repository.Translator
{
    public partial class WorkflowModelTranslator
    {
        #region Public functions
        /// <summary>
        /// Translate a list of models.
        /// </summary>
        /// <param name="DbDataReader">Opened datareader</param>
        /// <returns></returns>
        public static List<WorkflowModel> TranslateWorkflowModels(IDataReader DbDataReader)
        {
            var models = new List<WorkflowModel>();

            try
            {
                using (DbDataReader)
                {
                    // Read all rows
                    while (DbDataReader.Read())
                    {
                        WorkflowModel model = TranslateModel(DbDataReader);
                        models.Add(model);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return models;
        }

        /// <summary>
        /// Translate one model.
        /// </summary>
        /// <param name="DbDataReader">Opened datareader</param>
        /// <returns></returns>
        public static WorkflowModel TranslateWorkflowModel(IDataReader DbDataReader)
        {
            var model = new WorkflowModel();

            try
            {
                using (DbDataReader)
                {
                    if (DbDataReader.Read())
                        model = TranslateModel(DbDataReader);
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return model;
        }

        /// <summary>
        /// Translate workflow model actions.
        /// </summary>
        /// <param name="DbDataReader">Opened datareader</param>
        /// <returns></returns>
        public static List<WorkflowAction> TranslateActionsOrConditions(IDataReader DbDataReader)
        {
            var actions = new List<WorkflowAction>();

            try
            {
                using (DbDataReader)
                {
                    // Read all rows
                    while (DbDataReader.Read())
                    {
                        WorkflowAction action = new WorkflowAction();
                        action.Identifier = DbDataReader.GetLongValue("r_wkf_mdl_act_cond_id");
                        action.ActionOrConditionId = DbDataReader.GetLongValue("t_ref_wkf_act_cond_id"); 
                        action.Name = DbDataReader.GetStringValue("t_ref_wkf_act_cond_name");
                        action.Description = DbDataReader.GetStringValue("t_ref_wkf_act_cond_desc");
                        action.ClassName = DbDataReader.GetStringValue("t_ref_wkf_class_name");
                        action.AssemblyName = DbDataReader.GetStringValue("t_ref_wkf_assembly");
                        action.IsTrigger = false;
                        actions.Add(action);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return actions;
        }

        /// <summary>
        /// Translate workflow model actions.
        /// </summary>
        /// <param name="DbDataReader">Opened datareader</param>
        /// <returns></returns>
        public static List<WorkflowCondition> TranslateConditions(IDataReader DbDataReader)
        {
            var actions = new List<WorkflowCondition>();

            try
            {
                using (DbDataReader)
                {
                    // Read all rows
                    while (DbDataReader.Read())
                    {
                        WorkflowCondition condition = new WorkflowCondition();
                        condition.Identifier = DbDataReader.GetLongValue("r_wkf_mdl_act_cond_id");
                        condition.ActionOrConditionId = DbDataReader.GetLongValue("t_ref_wkf_act_cond_id");
                        condition.Name = DbDataReader.GetStringValue("t_ref_wkf_act_cond_name");
                        condition.Description = DbDataReader.GetStringValue("t_ref_wkf_act_cond_desc");
                        condition.ClassName = DbDataReader.GetStringValue("t_ref_wkf_class_name");
                        condition.AssemblyName = DbDataReader.GetStringValue("t_ref_wkf_assembly");
                        condition.IsTrigger = true;
                        actions.Add(condition);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return actions;
        }


        #endregion

        #region Private functions

        /// <summary>
        /// Translate one model.
        /// </summary>
        /// <param name="DbDataReader"></param>
        /// <returns></returns>
        private static WorkflowModel TranslateModel(IDataReader DbDataReader)
        {
            var model = new WorkflowModel();

            try
            {
                model.Identifier = Convert.ToInt32(DbDataReader["r_wkf_mdl_id"].ToString());
                model.WorkflowType = (WorkflowType)Enum.Parse(typeof(WorkflowType), DbDataReader["t_ref_wkf_type_id"].ToString()); 
                model.Name = DbDataReader["r_wkf_mdl_name"].ToString();
                model.Description = DbDataReader["r_wkf_mdl_desc"].ToString();
                model.ClassName = DbDataReader["r_wkf_mdl_classname"].ToString();
                model.AssemblyName = DbDataReader["r_wkf_mdl_assembly"].ToString();
                model.Rules = DbDataReader["r_wkf_mdl_rules"].ToString();
                model.IsSystem = Convert.ToBoolean(int.Parse(DbDataReader["r_wkf_mdl_system"].ToString()));
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return model;
        }
        #endregion
    }
}